package com.xcf.controller.business.wharfmanage;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReadStatus;
import org.jxls.reader.XLSReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.xcf.controller.base.BaseController;
import com.xcf.controller.base.ExportDataHandler;
import com.xcf.entity.Page;
import com.xcf.entity.business.common.FACTSEAL;
import com.xcf.service.business.wharfmanage.NumberEntryService;
import com.xcf.util.Const;
import com.xcf.util.DateUtil;
import com.xcf.util.FileUpload;
import com.xcf.util.ObjectExcelRead;
import com.xcf.util.PageData;
import com.xcf.util.PathUtil;
import com.xcf.util.ResponseResult;

import cn.com.xcf.dao.model.FactBookingNote;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value="/number")
public class NumberEntryController extends BaseController implements ExportDataHandler {
	@Resource
	private NumberEntryService numberentry;
	String menuUrl="number/entry";//菜单地址(权限用) 
	
	@RequestMapping(value="/entry")
	public ModelAndView number(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/business/wharfmanage/numberentry/numberentry");
		return mv;	
	}
	
	@RequestMapping(value="list")
	@ResponseBody
	public ResponseResult<Object> list(int page,int limit ) throws Exception{
		PageData pd = new PageData();
		pd=this.getPageData();
		PageHelper.startPage(page, limit);
		List<Map>list=numberentry.sear(pd);//查询托单表
		List<Map>list1= new ArrayList<>();
        for (Map map : list) {
        	if(map.get("BOX_TYPE_CONFIRM").toString().contains("&")){
               String arr[] =map.get("BOX_TYPE_CONFIRM").toString().split("&&");
               int smallNum=0;
  	           int bigNum=0;
               for (String BOx : arr) {
            	   String boxarry[] =BOx.split("X");
            	   int a=Integer.parseInt(boxarry[0]);
            	   String boxType = boxarry[1];
            	   boolean isSmallBox = boxType.contains("20");
            	   if(isSmallBox){
            		   smallNum+=a;
            	   }else{
            		   bigNum+=a; 
            	   }           	   				
			}
             map.put("SMALLBOX_NUM",smallNum);//小箱
  	         map.put("BIGBOX_NUM",bigNum);//大箱                  
        	}else{
        		 String boxTypeNum [] = ((String) map.get("BOX_TYPE_CONFIRM")).split(";");
            	 int smallNum=0;
    	         int bigNum=0;
    	         for(String box:boxTypeNum)
    	         {
    	        	String arr[]= box.split("X");
    	        	int num = Integer.parseInt(arr[0]);
    	        	String boxType = arr[1];
    	        	boolean isSmallBox = boxType.contains("20");
    	        	if(isSmallBox)
    	        	{
    	        		smallNum+=num;
    	        	}else{
    	        		bigNum+=num;
    	        	}
    	         }
    	         map.put("SMALLBOX_NUM",smallNum);//小箱
    	         map.put("BIGBOX_NUM",bigNum);//大箱
        	}
	             list1.add(map);
		}
		return ResponseResult.success(new PageInfo(list));
//		int total=list.size();
//		JSONArray arry = new JSONArray();		
//		for (PageData singlecarrier : list) {
//			JSONObject ob = new JSONObject();
//			ob.put("ID", singlecarrier.get("ID"));//ID
//			ob.put("BILL_CODE", singlecarrier.getString("BILL_CODE"));//托单编号
//			ob.put("LADING_BILL_CODE", singlecarrier.getString("LADING_BILL_CODE"));//提单编号
//			ob.put("BOX_TYPE_CONFIRM", singlecarrier.getString("BOX_TYPE_CONFIRM"));//箱型确定
			//String BOX_TYPE_CONFIRM=singlecarrier.getString("BOX_TYPE_CONFIRM");
//			ob.put("BOX_REQUIREMENT", singlecarrier.getString("BOX_REQUIREMENT"));//放箱要求
//			ob.put("CONTAINER_OWNER", singlecarrier.getString("CONTAINER_OWNER"));//箱主
//			ob.put("BOAT_NAME", singlecarrier.getString("BOAT_NAME"));//江船船名
//			ob.put("BOAT_VOYAGE_NAME", singlecarrier.getString("BOAT_VOYAGE_NAME"));//江船航次
//			ob.put("RIVER_PORT", singlecarrier.getString("RIVER_PORT"));//江船起运港
//			ob.put("RIVER_UNLOAD", singlecarrier.getString("RIVER_UNLOAD"));//江船卸区
//			ob.put("PORT_DISCHARGE", singlecarrier.getString("PORT_DISCHARGE"));//目的港
			//if(singlecarrier.get("ESTIMATE_TIME")!=null&&singlecarrier.get("ESTIMATE_TIME")!=""){
				//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				//String ESTIMATE_TIME = sdf.format(singlecarrier.get("ESTIMATE_TIME"));
				//ob.put("ESTIMATE_TIME",ESTIMATE_TIME );//预计放箱时间
			//}else{
				//ob.put("ESTIMATE_TIME",singlecarrier.getString("ESTIMATE_TIME"));//预计放箱时间
			//}
			
//			ob.put("BUSINESS_TYPE", singlecarrier.getString("BUSINESS_TYPE"));//贸易类型,业务类型
//			ob.put("SALER_NAME", singlecarrier.getString("SALER_NAME"));//业务员,销售人员姓名
//			ob.put("ENCASEMENT_METHOD", singlecarrier.getString("ENCASEMENT_METHOD"));//装箱方式
//			singlecarrier.put("CREATE_TIME", singlecarrier.get("CREATE_TIME"));//接单时间
//			ob.put("BILL_INPUTER", singlecarrier.getString("BILL_INPUTER"));//操作员
//			ob.put("TRANSPORT_CLAUSE", singlecarrier.getString("TRANSPORT_CLAUSE"));//运输条款
//			         String boxTypeNum[] = singlecarrier.getString("BOX_TYPE_CONFIRM").split(";");
//			         int smallNum=0;
//			         int bigNum=0;
//			         for(String box:boxTypeNum)
//			         {
//			        	String arr[]= box.split("X");
//			        	int num = Integer.parseInt(arr[0]);
//			        	String boxType = arr[1];
//			        	boolean isSmallBox = boxType.contains("20");
//			        	if(isSmallBox)
//			        	{
//			        		smallNum+=num;
//			        	}else{
//			        		bigNum+=num;
//			        	}
//			         }
//			String arr[]=singlecarrier.getString("BOX_TYPE_CONFIRM").split("X");
//			boolean  IsSmallbox=arr[1].contains("20");
			         
//			         ob.put("SMALLBOX_NUM",smallNum);//小箱
//			         ob.put("BIGBOX_NUM",bigNum);//大箱
//			ob.put("CHINESE_NAME", singlecarrier.getString("CHINESE_NAME"));//中文品名
//			ob.put("GOODS_NUM", singlecarrier.getString("GOODS_NUM"));//件数
//			ob.put("PACK_TYPE", singlecarrier.getString("PACK_TYPE"));//包装类别
//			ob.put("GOODS_GROSS_WEIGHT", singlecarrier.getString("GOODS_GROSS_WEIGHT"));//毛重
//			ob.put("VOLUME", singlecarrier.getString("VOLUME"));//总体积
//			ob.put("GOODS_TYPE", singlecarrier.getString("GOODS_TYPE"));//货物种类
//			//singlecarrier.put("ESTIMATE_RIVER_BOAT_TIME", value);//装船期限
//			ob.put("SHIPPER", singlecarrier.getString("SHIPPER"));//委托单位
//			ob.put("CONTAINER_NO", singlecarrier.getString("CONTAINER_NO"));//箱号
//			ob.put("SEAL_NO", singlecarrier.getString("SEAL_NO"));//封号
//			ob.put("FORWARDER_CUSTOMER_SERVICE", singlecarrier.getString("FORWARDER_CUSTOMER_SERVICE"));//货代客服
//			ob.put("SHIPPER_NUM", singlecarrier.getString("SHIPPER_NUM"));//托运人自编号
//			arry.add(ob);
//			}  
//		response.setContentType("text/html; charset=utf-8");         
//        response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+total+"\""+","+"\""+"rows"+"\""+":"+arry+"}");
	}
	
	//船名
	@RequestMapping(value="ship")
	public  void aa(HttpServletResponse response) throws Exception{
		PageData pd = new PageData();
		pd=this.getPageData();
		List<PageData>ship=numberentry.searship(pd);//查询船名
	
		JSONArray arry = new JSONArray();
		for (PageData sh : ship) {
			JSONObject aa = new JSONObject();
			aa.put("ID", sh.get("ID"));
			aa.put("SHIP_NAME", sh.getString("SHIP_NAME"));
			arry.add(aa);
		}	
	response.setContentType("text/html; charset=utf-8");         
    response.getWriter().write("{"+"\""+"total2"+"\""+":"+"\""+"50"+"\""+","+"\""+"rows2"+"\""+":"+arry+"}");
	}
	//航次
	@RequestMapping(value="vo")
	public void vo(HttpServletResponse response) throws Exception{
		PageData pd =  new PageData();
		pd=this.getPageData();
	List<PageData>voyage=numberentry.searvoyage(pd);//查询航次
	JSONArray arry = new JSONArray();
	for (PageData vo : voyage) {
	JSONObject jj = new JSONObject();
	jj.put("VOYAGE_NUMBER", vo.getString("VOYAGE_NUMBER"));
       arry.add(jj);
}
	response.setContentType("text/html; charset=utf-8");         
    response.getWriter().write("{"+"\""+"total3"+"\""+":"+"\""+"50"+"\""+","+"\""+"rows3"+"\""+":"+arry+"}");
	}
	
	//箱封号录入
	@RequestMapping(value="/sealentry")
	@ResponseBody
	public ResponseResult  sealentry(int page,int limit) throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		PageHelper.startPage(page, limit);
		List<PageData> list=numberentry.searfeng(pd);
		return ResponseResult.success(new PageInfo(list));
//		int total=list.size();
//		JSONArray  arry = new JSONArray();
//		for (PageData se : list) {
//			JSONObject ob = new JSONObject();
//		     ob.put("ID", se.get("ID"));//ID
//		     ob.put("BILL_CODE", se.getString("BILL_CODE"));//托单号
//		     ob.put("LADING_BILL_CODE", se.getString("LADING_BILL_CODE"));//提单号
//		     String isDivide=null;
//		     if(se.get("IS_DIVIDE_BOX")==null||se.get("IS_DIVIDE_BOX").toString().equals("0")||se.get("IS_DIVIDE_BOX").equals("false")){
//		    	 isDivide="否"; 
//		     }else{
//		    	 isDivide="是"; 
//		     }
//		     ob.put("IS_DIVIDE_BOX", isDivide);//是否分箱
//		     ob.put("HARBOUR_REPORT_STATE", se.getString("HARBOUR_REPORT_STATE"));//离港报状态
//		     ob.put("SUITCASE_STATION_ID", se.get("SUITCASE_STATION_ID"));//提箱场站ID
//		     ob.put("STATION_NAME", se.getString("STATION_NAME"));//提箱场站
//		     ob.put("CONTAINER_OWNER", se.getString("CONTAINER_OWNER"));//箱主
//		     ob.put("CONTAINER_TYPE", se.getString("CONTAINER_TYPE"));//箱型
//		     ob.put("CONTAINER_NUM", se.getString("CONTAINER_NUM"));//箱号
//		     ob.put("SEAL_NO", se.getString("SEAL_NO"));//封号
//		     ob.put("CUSTOMS_SEAL", se.getString("CUSTOMS_SEAL"));//海关封
//		     ob.put("PACKAGE_NUM", se.get("PACKAGE_NUM"));//件数
//		     ob.put("GROSS_WEIGHT", se.get("GROSS_WEIGHT"));//毛重
//		     ob.put("CAPACITY", se.get("CAPACITY"));//体积
//		    if(se.get("EMPTY_ENTRY_DATE")!=null&&se.get("EMPTY_ENTRY_DATE")!=""){
//		     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
//		     String str=sdf.format(se.get("EMPTY_ENTRY_DATE"));
//		     ob.put("EMPTY_ENTRY_DATE",str );//空进
//		    }else{
//		    	 ob.put("EMPTY_ENTRY_DATE",se.get("EMPTY_ENTRY_DATE") );//空进
//		    }
//		    if(se.get("EMPTY_OUT_DATE")!=null&&se.get("EMPTY_OUT_DATE")!=""){
//		     SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd"); 
//		     String d=a.format(se.get("EMPTY_OUT_DATE"));
//		     ob.put("EMPTY_OUT_DATE",d );//空出
//		    }else{
//		    	 ob.put("EMPTY_OUT_DATE",se.get("EMPTY_OUT_DATE") );//空出
//		    }
//		    if(se.get("WEIGHT_ENTRY_DATE")!=null&&se.get("WEIGHT_ENTRY_DATE")!=""){
//			 SimpleDateFormat b = new SimpleDateFormat("yyyy-MM-dd"); 
//		     String e=b.format(se.get("WEIGHT_ENTRY_DATE"));
//			 ob.put("WEIGHT_ENTRY_DATE",e );//重进
//		    }else{
//		    	 ob.put("WEIGHT_ENTRY_DATE",se.get("WEIGHT_ENTRY_DATE") );//重进
//		    }
//		    if(se.get("WEIGHT_OUT_DATE")!=null&&se.get("WEIGHT_OUT_DATE")!=""){
//			 SimpleDateFormat c = new SimpleDateFormat("yyyy-MM-dd"); 
//		     String f=c.format(se.get("WEIGHT_OUT_DATE"));
//			 ob.put("WEIGHT_OUT_DATE",f );//重出
//		    }else{
//		    	 ob.put("WEIGHT_OUT_DATE",se.get("WEIGHT_OUT_DATE") );//重出
//		    }
//		   ob.put("OVER_LIMIT", se.get("OVER_LIMIT"));//超限
//		   ob.put("OVER_HEIGHT", se.get("OVER_HEIGHT"));//超高
//		   ob.put("FRONT_OVER", se.get("FRONT_OVER"));//前超
//		   ob.put("BEHIND_OVER", se.get("BEHIND_OVER"));//后超
//		   ob.put("LEFT_OVER", se.get("LEFT_OVER"));//左超
//		   ob.put("RIGHT_OVER", se.get("RIGHT_OVER"));//右超
//		   ob.put("TURN_BOX_UNIT", se.getString("TURN_BOX_UNIT"));//转箱单位
//		   ob.put("PACKING_METHOD", se.getString("PACKING_METHOD"));//装箱方式
//		   ob.put("CUSTOMS_DECLARATION_NUM", se.getString("CUSTOMS_DECLARATION_NUM"));//报关单号
//		   String islogistics=null;
//		   if(se.get("IS_LOGISTICS_BOX")==null||se.get("IS_LOGISTICS_BOX").toString().equals("0")||se.get("IS_LOGISTICS_BOX").equals("false")){
//			   islogistics="否";
//		   }else{
//			   islogistics="是";
//		   }
//		   ob.put("IS_LOGISTICS_BOX",islogistics );//是否物流用
//		   String isspell = null;
//		   if(se.get("IS_SPELL_BOX")==null||se.get("IS_SPELL_BOX").toString().equals("0")){
//			   isspell="否";
//		   }else{
//			   isspell="是";
//		   }
//		   ob.put("IS_SPELL_BOX",isspell );//是否拼箱
//		   String isempty=null;
//		   if(se.get("IS_EMPTY_BOX")==null||se.get("IS_EMPTY_BOX").toString().equals("0")){
//			   isempty="否"; 
//		   }else{
//			   isempty="是";
//		   }
//		   ob.put("IS_EMPTY_BOX",isempty );//是否空箱
//		   String status=null;
//		   if(se.get("STATUS").toString().equals("1")){
//			   status="是"; 
//		   }else{
//			   status="否";
//		   }
//		   ob.put("STATUS",status );//是否放箱
//		     arry.add(ob);
//			
//		}
//		response.setContentType("text/html; charset=utf-8");         
//	    response.getWriter().write("{"+"\""+"total1"+"\""+":"+"\""+total+"\""+","+"\""+"rows1"+"\""+":"+arry+"}");
	}
	//保存及修改
	@RequestMapping(value="/save")
	@ResponseBody
	public String save() throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();		
		numberentry.upd(pd);
		numberentry.edit(pd);
		return null;
	}

	//箱封号保存
	@SuppressWarnings("unused")
	@RequestMapping(value="/Preservation")
	@ResponseBody
	public String preservation() throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		PageData a=numberentry.seraxiang(pd);//查询封箱号里面是否有无重复数据
		if(a==null){
			String BILL_CODE=pd.getString("BILL_CODE");//托单编号
			String LADING_BILL_CODE=pd.getString("LADING_BILL_CODE");//提单编号
			String CONTAINER_NO=pd.getString("CONTAINER_NO");//箱号
			String SEAL_NO=pd.getString("SEAL_NO");//封号
			String  GOODS_NUM=pd.getString("GOODS_NUM");//件数
			String  GOODS_GROSS_WEIGHT=pd.getString("GOODS_GROSS_WEIGHT");//毛重
			String BOX_TYPE=pd.getString("BOX_TYPE");//箱型
			String aa=null;
			String bb=null;
	        if( pd.getString("dasd").equals("L")){
	        	 aa="1";
	        }else if(pd.getString("dasd").equals("A")){
	        	bb="1";
	        }else if(pd.getString("dasd").equals("E")){
	        	aa="0";
	        	bb="0";
	        }	       
		     PageData ob = new PageData();
		     ob.put("BILL_CODE", BILL_CODE);//托单编号
		     ob.put("LADING_BILL_CODE", LADING_BILL_CODE);//提单编号
		     ob.put("CONTAINER_NO",CONTAINER_NO );//箱号
		     ob.put("SEAL_NO", SEAL_NO);//封号
		     ob.put("GOODS_NUM", GOODS_NUM);//件数
		     ob.put("GOODS_GROSS_WEIGHT", GOODS_GROSS_WEIGHT);//毛重
		     ob.put("BOX_TYPE", BOX_TYPE);//箱型
		     ob.put("zhuangtai", aa);//是否拼箱状态
		     ob.put("bb", bb);//是否空箱状态  
		     numberentry.add(ob);//新增封箱号
	         numberentry.Upde(ob);//新增托单表里面的封号
			
		}else{
			String BILL_CODE=pd.getString("BILL_CODE");//托单编号
			String LADING_BILL_CODE=pd.getString("LADING_BILL_CODE");//提单编号
			String CONTAINER_NO=pd.getString("CONTAINER_NO");//箱号
			String SEAL_NO=pd.getString("SEAL_NO");//封号
			String  GOODS_NUM=pd.getString("GOODS_NUM");//件数
			String  GOODS_GROSS_WEIGHT=pd.getString("GOODS_GROSS_WEIGHT");//毛重
			String BOX_TYPE=pd.getString("BOX_TYPE");//箱型
			String aa=null;
			String bb=null;
			 if( pd.getString("dasd").equals("L")){
	        	 aa="1";
	        }else if(pd.getString("dasd").equals("A")){
	        	bb="1";
	        }else if(pd.getString("dasd").equals("E")){
	        	aa="0";
	        	bb="0";
	        }
		     PageData ob = new PageData();
		     ob.put("BILL_CODE", BILL_CODE);//托单编号
		     ob.put("LADING_BILL_CODE", LADING_BILL_CODE);//提单编号
		     ob.put("CONTAINER_NO",CONTAINER_NO );//箱号
		     ob.put("SEAL_NO", SEAL_NO);//封号
		     ob.put("GOODS_NUM", GOODS_NUM);//件数
		     ob.put("GOODS_GROSS_WEIGHT", GOODS_GROSS_WEIGHT);//毛重
		     ob.put("BOX_TYPE", BOX_TYPE);//箱型
		     ob.put("zhuangtai", aa);//是否拼箱状态
		     ob.put("bb", bb);//是否空箱状态
		     numberentry.Edit(ob);//修改脱单表封箱号
	         numberentry.Upde(ob);//新增托单表里面的封号
			
		}
		
		return null;
		
	}
	//导入
	@RequestMapping(value="num")
	@ResponseBody
	public Object impBookingCargo(@RequestParam("file") CommonsMultipartFile file) {
		
        try {
        	String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE;
			String fileName =  FileUpload.fileUp(file, filePath, "test");	
			List<PageData> listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 1, 0, 0);
			PageData  pd = new PageData();
			boolean updateRes = false;
			for (PageData phq : listPd) {
				pd.put("BILL_CODE", phq.getString("var0")); //托单编号
	        	pd.put("LADING_BILL_CODE", phq.getString("var1"));//提单编号
	        	int asca=0;
	        	if(phq.getString("var2").equals("是")){
	        		 asca=1;
	        	}else{
	        		asca=0;
	        	}	
	        	pd.put("IS_DIVIDE_BOX",asca);//是否分箱
	        	pd.put("HARBOUR_REPORT_STATE", phq.getString("var3"));//离港报状态 1
	        	pd.put("CONTAINER_OWNER", phq.getString("var4"));//箱主
	        	pd.put("CONTAINER_TYPE", phq.getString("var5"));//箱型
	        	pd.put("CONTAINER_NUM", phq.getString("var6"));//箱号
	        	pd.put("SEAL_NO", phq.getString("var7"));//封号
	        	pd.put("CUSTOMS_SEAL", phq.getString("var8"));//海关封
	        	pd.put("PACKAGE_NUM", phq.getString("var9"));//件数
	        	pd.put("GROSS_WEIGHT", phq.get("var10"));//毛重
	        	pd.put("CAPACITY", phq.get("var11"));//体积 
	        	
	        	if(phq.get("var12")!=null||phq.get("var12")==""){
	   		     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	   		     java.util.Date str=sdf.parse(phq.get("var12").toString());
	   		      pd.put("EMPTY_ENTRY_DATE",str );//空进
	           	}else{
	           		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	       		     java.util.Date str=sdf.parse(phq.get("var12").toString());
	           		pd.put("var12",str);//空进
	           	}
	           	if(phq.get("var13")!=null&&phq.get("var13")!=""){
	      		     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	      		     java.util.Date str=sdf.parse(phq.get("var13").toString());
	      		      pd.put("EMPTY_OUT_DATE",str );//空出
	              	}else{
	              		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	          		     java.util.Date str=sdf.parse(phq.get("var13").toString());
	              		pd.put("EMPTY_ENTRY_DATE",str);//空出
	              	}
	           	if(phq.get("var14")!=null&&phq.get("var14")!=""){
	      		     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	      		     java.util.Date str=sdf.parse(phq.get("var14").toString());
	      		      pd.put("WEIGHT_ENTRY_DATE",str );//重进
	              	}else{
	              		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	          		     java.util.Date str=sdf.parse(phq.get("var14").toString());
	              		pd.put("WEIGHT_ENTRY_DATE",str);//重进
	              	}
	           	if(phq.get("var15")!=null&&phq.get("var15")!=""){
	      		     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	      		     java.util.Date str=sdf.parse(phq.get("var15").toString());
	      		      pd.put("WEIGHT_OUT_DATE",str );//重出
	              	}else{
	              		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
	          		     java.util.Date str=sdf.parse(phq.get("var15").toString());
	              		pd.put("WEIGHT_OUT_DATE",str);//重出
	              	}
	             pd.put("OVER_LIMIT", phq.get("var16"));//超限
	             pd.put("OVER_HEIGHT", phq.get("var17"));
	             pd.put("FRONT_OVER", phq.get("var18"));
	             pd.put("BEHIND_OVER", phq.get("var19"));
	             pd.put("TURN_BOX_UNIT", phq.get("var20"));//转型单位
	             pd.put("PACKING_METHOD", phq.get("var21"));
	             int aa=0;
	          	if(phq.get("var22").equals("是")){
	          		 aa=1;
	          	}else{
	          		aa=0;
	          	}
	          	pd.put("IS_LOGISTICS_BOX",aa);//是否物流
	          	int bb=0;
	          	if(phq.get("var23").equals("是")){
	          		 bb=1;
	          	}else{
	          		bb=0;
	          	}
	          	pd.put("IS_SPELL_BOX",bb);//是否拼箱
	          	
	          	int cc=0;
	          	if(phq.get("var24").equals("是")){
	          		 cc=1;
	          	}else{
	          		cc=0;
	          	}
	          	pd.put("IS_EMPTY_BOX",cc);//是否空箱
	          	if(phq != null&& StringUtils.isNotBlank(phq.getString("var0"))){
					PageData tuo=numberentry.seartuo(pd);
					if(tuo!=null){
						 List<PageData> a= numberentry.cha(pd);
						 if(a.size()==0||a==null){
							updateRes= numberentry.add1(pd);
//								if(updateRes){
//									return ResponseResult.success(phq.getString("var0"));
//
//						         }
							}else{
								 updateRes= numberentry.updateInfo(pd);
//								 if(updateRes){
//									return ResponseResult.success(phq.getString("var0"));
//
//							                }
					               }
					       }			
				}
	          	
			}
             if(updateRes){
            	 return ResponseResult.success(listPd); 
             }else{
            	 logger.error("导入封箱号操作失败 " ); 
             }
			
		} catch (Exception e) {
			logger.error("读取上传文件失败, " ,e);
			e.printStackTrace();
		}
		return ResponseResult.fail("导入文件失败");
	}
	
//	private FACTSEAL readExcel(CommonsMultipartFile file) throws Exception {
//		InputStream inputXML = new BufferedInputStream(getClass().getResourceAsStream("/template/excel/num.xml"));
//		XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
//		InputStream inputXLS = new BufferedInputStream(file.getInputStream());
//		Map beans = new HashMap();
//		FACTSEAL fact = new FACTSEAL();
//		beans.put("fact", fact);
//		XLSReadStatus readStatus = mainReader.read( inputXLS, beans);
//		if(readStatus.isStatusOK()){
//			return fact;
//		}
//		return null;
//	}

	      //报关配船导入
		@RequestMapping(value="customs_declaration")
		@ResponseBody
		public Object impBoo(@RequestParam("file") CommonsMultipartFile file) {
	        try {
	        	String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE;
				String fileName =  FileUpload.fileUp(file, filePath, "test");	
				List<PageData> listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 7, 1, 0);
				PageData  pd = new PageData();
				boolean updateRes = false;
				for (PageData phq : listPd) {	
					pd.put("BILL_CODE", phq.getString("var2")); //托单编号
		        	pd.put("LADING_BILL_CODE", phq.getString("var1"));//提单编号
		        	pd.put("PACKAGE_NUM", phq.getString("var8"));//件数
		        	pd.put("GROSS_WEIGHT", phq.get("var9"));//毛重
		        	pd.put("CAPACITY", phq.get("var10"));//体积 
		        	pd.put("CONTAINER_OWNER", phq.getString("var11"));//箱主
		        	pd.put("CUSTOMS_DECLARATION_NUM", phq.getString("var22"));//报关单号
		        	pd.put("SEAL_NO", phq.getString("var23"));//封号
		        	if(phq != null&& StringUtils.isNotBlank(phq.getString("var2"))){
						PageData tuo=numberentry.seartuo(pd);
						if(tuo!=null){
							 List<PageData> a= numberentry.cha(pd);
							 if(a.size()==0||a==null){
									 updateRes= numberentry.add2(pd);
//									if(updateRes){
//										return ResponseResult.success(phq.getString("BILL_CODE"));
//
//							         }
								}else{
									 updateRes= numberentry.updateInfo2(pd);
//									 if(updateRes){
//											return ResponseResult.success(fa.getBILL_CODE());
//
//								}
						}
						}
				    
					
					}
				}
				 if(updateRes){
	            	 return ResponseResult.success(listPd); 
	             }else{
	            	 logger.error("导入封箱号操作失败 " ); 
	             }
				
			} catch (Exception e) {
				logger.error("读取上传文件失败, " ,e);
				e.printStackTrace();
			}
			return ResponseResult.fail("导入文件失败");
		}
//	private FACTSEAL readExcel1(CommonsMultipartFile file) throws Exception {
//		InputStream inputXML = new BufferedInputStream(getClass().getResourceAsStream("/template/excel/customs_declaration.xml"));
//		XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
//		InputStream inputXLS = new BufferedInputStream(file.getInputStream());
//		Map beans = new HashMap();
//		FACTSEAL fact = new FACTSEAL();
//		beans.put("fact", fact);
//		XLSReadStatus readStatus = mainReader.read( inputXLS, beans);
//		if(readStatus.isStatusOK()){
//			return fact;
//		}
//		return null;
//	}
		  //报关配船报文导入
				@RequestMapping(value="message")
				@ResponseBody
				public Object ADimpBoo(@RequestParam("file") CommonsMultipartFile file) {
			        try {
	
			        	String filePath = PathUtil.getClasspath() + Const.FILEPATHFILE;
						String fileName =  FileUpload.fileUp(file, filePath, "test");	
						List<PageData> listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 6, 1, 0);
						PageData  pd = new PageData();
						boolean updateRes = false;
						for (PageData phq : listPd) {
						 	pd.put("LADING_BILL_CODE", phq.getString("var1"));//提单编号
						 	pd.put("CUSTOMS_DECLARATION_NUM", phq.getString("var2"));//报关单号
				        	pd.put("CONTAINER_NUM", phq.getString("var3"));//箱号
				        	pd.put("SEAL_NO", phq.getString("var4"));//封号
				        	
				        	int ci =Integer.parseInt(phq.get("var5").toString());//尺寸
				        	String xiang=phq.getString("var6");//箱类
				        	String aaaa=ci+xiang;//箱型 字符串的拼接
				        	pd.put("CONTAINER_TYPE",aaaa );//箱型
				        	int kong=0;
				        	int ping=0;
				        	String zhuang=phq.getString("var7");
				             if(zhuang.equals("F")){
				            	  kong=0;
				            	  ping=0;
				             }else if(zhuang.equals("E")){
				            	  kong=1;
				            	  ping=0;
				             }else if(zhuang.equals("A")){
				            	 kong=0;
				            	  ping=1;
				             }
				             pd.put("kong", kong);//是否空箱
				             pd.put("ping", ping);//是否拼箱
		
				        	pd.put("CONTAINER_OWNER", phq.getString("var8"));//箱主
				        	pd.put("PACKAGE_NUM", phq.get("var12"));//件数
				        	pd.put("GROSS_WEIGHT", phq.get("var14"));//毛重
				        	
							if(phq != null&& StringUtils.isNotBlank(phq.getString("var1"))){
								//PageData tuo=numberentry.seartuo(pd);
								//if(tuo!=null){
									 List<PageData> a= numberentry.fengxiang(pd);
									 if(a.size()>0){
										 updateRes= numberentry.updateInfo3(pd);
											 //if(updateRes){
												//	return ResponseResult.success(phq.getString("var1"));
		
										  //}
								
								   }
						    
							
							 }
						}
			       
						if(updateRes){
			            	 return ResponseResult.success(listPd); 
			             }else{
			            	 logger.error("导入封箱号操作失败 " ); 
			             }
			        
						logger.error("导入封箱号操作失败 " );
					} catch (Exception e) {
						logger.error("读取上传文件失败, " ,e);
						e.printStackTrace();
					}
					return ResponseResult.fail("导入文件失败");
				}
	
				//单个导入文件
//	  //报关配船报文导入
//			@RequestMapping(value="message")
//			@ResponseBody
//			public Object ADimpBoo(@RequestParam("file") CommonsMultipartFile file) {
//		        try {
//
//		        	FACTSEAL fa =  readExcel2(file);
//		        	PageData pd = new PageData();
//		        	pd.put("LADING_BILL_CODE", fa.getLADING_BILL_CODE());//提单编号
//		        	pd.put("CONTAINER_NUM", fa.getCONTAINER_NUM());//箱号
//		        	pd.put("SEAL_NO", fa.getSEAL_NO());//封号
//		        	int ci=fa.getChicun();//尺寸
//		        	String xiang=fa.getXianglei();//箱类
//		        	String aaaa=ci+xiang;//箱型 字符串的拼接
//		        	pd.put("CONTAINER_TYPE",aaaa );//箱型
//		        	int kong=0;
//		        	int ping=0;
//		        	String zhuang=fa.getXiangzhuangtai();
//		             if(zhuang.equals("F")){
//		            	  kong=0;
//		            	  ping=0;
//		             }else if(zhuang.equals("E")){
//		            	  kong=1;
//		            	  ping=0;
//		             }else if(zhuang.equals("A")){
//		            	 kong=0;
//		            	  ping=1;
//		             }
//		             pd.put("kong", kong);//是否空箱
//		             pd.put("ping", ping);//是否拼箱
//
//		        	pd.put("CONTAINER_OWNER", fa.getCONTAINER_OWNER());//箱主
//		        	//pd.put("PACKAGE_NUM", fa.getPACKAGE_NUM());//件数
//		        	//pd.put("GROSS_WEIGHT", fa.getGROSS_WEIGHT());//毛重
//		        	//pd.put("CAPACITY", fa.getCAPACITY());//体积 
//		        	//pd.put("CUSTOMS_DECLARATION_NUM", fa.getCUSTOMS_DECLARATION_NUM());//报关单号
//		        	
//					if(fa != null&& StringUtils.isNotBlank(fa.getLADING_BILL_CODE())){
//						//PageData tuo=numberentry.seartuo(pd);
//						//if(tuo!=null){
//							 List<PageData> a= numberentry.fengxiang(pd);
//							 if(a.size()>0){
//									boolean updateRes= numberentry.updateInfo3(pd);
//									 if(updateRes){
//											return ResponseResult.success(fa.getBILL_CODE());
//
//								  }
//						
//						   }
//				    
//					
//					 }
//					logger.error("导入封箱号操作失败 " );
//				} catch (Exception e) {
//					logger.error("读取上传文件失败, " ,e);
//					e.printStackTrace();
//				}
//				return ResponseResult.fail("导入文件失败");
//			}
//		private FACTSEAL readExcel2(CommonsMultipartFile file) throws Exception {
//			InputStream inputXML = new BufferedInputStream(getClass().getResourceAsStream("/template/excel/message.xml"));
//			XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
//			InputStream inputXLS = new BufferedInputStream(file.getInputStream());
//			Map beans = new HashMap();
//			FACTSEAL fact = new FACTSEAL();
//			beans.put("fact", fact);
//			XLSReadStatus readStatus = mainReader.read( inputXLS, beans);
//			if(readStatus.isStatusOK()){
//				return fact;
//			}
//			return null;
//		}
	
	//导出到excel
	@Override
	public List getData(String type, Map param, Page page) {
		PageData pd = new PageData();
		try {
		if(MapUtils.isEmpty(param) || param.get("BILL_CODE") == null || StringUtils.isBlank(param.get("BILL_CODE").toString())){
			return Lists.newArrayList();
		}
		if("numberentry".equalsIgnoreCase(type)){

		}
		List<PageData> list= numberentry.queryByBillCode(param);
		JSONArray array = new JSONArray();
		  for (PageData data : list) {
			  JSONObject ob = new JSONObject();
			  ob.put("BILL_CODE", data.getString("BILL_CODE"));
			  ob.put("LADING_BILL_CODE", data.getString("LADING_BILL_CODE"));
			  ob.put("HARBOUR_REPORT_STATE", data.getString("HARBOUR_REPORT_STATE"));
			  ob.put("SUITCASE_STATION", data.getString("SUITCASE_STATION"));
			
			  ob.put("CONTAINER_OWNER", data.getString("CONTAINER_OWNER"));
			  ob.put("CONTAINER_TYPE", data.getString("CONTAINER_TYPE"));
			  ob.put("CONTAINER_NUM", data.getString("CONTAINER_NUM"));
			  ob.put("SEAL_NO", data.getString("SEAL_NO"));
			  ob.put("CUSTOMS_SEAL", data.getString("CUSTOMS_SEAL"));
			  ob.put("PACKAGE_NUM", data.getString("PACKAGE_NUM"));
			  ob.put("GROSS_WEIGHT", data.get("GROSS_WEIGHT"));
			  ob.put("CAPACITY", data.get("CAPACITY"));
			  ob.put("TURN_BOX_UNIT", data.getString("TURN_BOX_UNIT"));
			  ob.put("PACKING_METHOD", data.getString("PACKING_METHOD"));
			  if(data.get("EMPTY_ENTRY_DATE")!=null &&data.get("EMPTY_ENTRY_DATE")!=""){
				  String a=data.get("EMPTY_ENTRY_DATE").toString();
					ob.put("EMPTY_ENTRY_DATE", a);  
			  }else{
				  ob.put("EMPTY_ENTRY_DATE", data.get("EMPTY_ENTRY_DATE"));
			  }
			  
			  if(data.get("EMPTY_OUT_DATE")!=null &&data.get("EMPTY_OUT_DATE")!=""){
				  String a=data.get("EMPTY_OUT_DATE").toString();
					ob.put("EMPTY_OUT_DATE", a);  
			  }else{
				  ob.put("EMPTY_OUT_DATE", data.get("EMPTY_OUT_DATE"));
			  }
		      if(data.get("WEIGHT_ENTRY_DATE")!=null&&data.get("WEIGHT_ENTRY_DATE")!=""){
		    	  String c=data.get("WEIGHT_ENTRY_DATE").toString();
		    	  ob.put("WEIGHT_ENTRY_DATE", c);
		      }else{
		    	  ob.put("WEIGHT_ENTRY_DATE", data.get("WEIGHT_ENTRY_DATE"));
		      }
				if(data.get("WEIGHT_OUT_DATE")!=null && data.get("WEIGHT_OUT_DATE")!=""){
					String d=data.get("WEIGHT_OUT_DATE").toString();
					ob.put("WEIGHT_OUT_DATE", d);
				}	else{
					ob.put("WEIGHT_OUT_DATE", data.get("WEIGHT_OUT_DATE"));
				}	
			if(data.get("IS_DIVIDE_BOX")==null||data.get("IS_DIVIDE_BOX").toString().equals("0")||data.get("IS_DIVIDE_BOX")==""){
				String is_divide="否";
				ob.put("IS_DIVIDE_BOX", is_divide);
			}else{
				String is_divide="是";
				ob.put("IS_DIVIDE_BOX", is_divide);
			}
			if(data.get("IS_LOGISTICS_BOX")==null||data.get("IS_LOGISTICS_BOX").toString().equals("0")||data.get("IS_LOGISTICS_BOX")==""){
				String is_divide="否";
				ob.put("IS_LOGISTICS_BOX", is_divide);
			}else{
				String is_divide="是";
				ob.put("IS_LOGISTICS_BOX", is_divide);
			}
			if(data.get("IS_SPELL_BOX")==null||data.get("IS_SPELL_BOX").toString().equals("0")||data.get("IS_SPELL_BOX")==""){
				String is_divide="否";
				ob.put("IS_SPELL_BOX", is_divide);
			}else{
				String is_divide="是";
				ob.put("IS_SPELL_BOX", is_divide);
			}
			if(data.get("IS_EMPTY_BOX")==null||data.get("IS_EMPTY_BOX").toString().equals("0")||data.get("IS_EMPTY_BOX")==""){
				String is_divide="否";
				ob.put("IS_EMPTY_BOX", is_divide);
			}else{
				String is_divide="是";
				ob.put("IS_EMPTY_BOX", is_divide);
			}
			if(data.get("STATUS")==null||data.get("STATUS").toString().equals("0")||data.get("STATUS")==""){
				String is_divide="否";
				ob.put("STATUS", is_divide);
			}else{
				String is_divide="是";
				ob.put("STATUS", is_divide);
			}
			array.add(ob);
		}
		  return Lists.newArrayList(array);	
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
		
	}

	@Override
	public String getService() {
		return  getClass().getSimpleName();
	}

	@Override
	public Map<String, String> setNames() {
		Map m = new HashMap(3);
		m.put("numberEntry","numberEntry.xls");
		m.put("numberEntryName","封箱号录入("+ DateUtil.getSdfTimes()+").xls");
		m.put("numberEntryModule",getService());
		return m;
	}
	

}